var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);
//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            field: 'selectItem',
            radio: true
        },
        {
            field: 'orderno',
            title: '订单号',
            align: 'center',
            valign: 'middle',
            visible: false,
        },
        {
            field: 'nickname',
            title: '姓名',
            align: 'center',
            valign: 'middle',
        },{
            field: 'tel',
            title: '电话',
            align: 'center',
            valign: 'middle',
        },{
            field: 'product_name',
            title: '商品名称',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'orderType',
            title: '订单类别',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'orderStruts',
            title: '订单状态',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'totalamount',
            title: '应付金额',
            align: 'center',
            valign: 'middle',
            visible: false,
            formatter: function (value) {
                if (empty(value))
                    return "0 元";
                // var price = parseFloat(value / 100).toFixed(2);
                return value + " 元";
            }
        }, {
            field: 'coupon_money',
            title: '折扣金额',
            align: 'center',
            valign: 'middle',
            visible: false,
            formatter: function (value) {
                if (empty(value))
                    return "0 元";
                // var price = parseFloat(value / 100).toFixed(2);
                return value + " 元";
            }
        }, {
            field: 'payment',
            title: '实付金额',
            align: 'center',
            valign: 'middle',
            visible: false,
            formatter: function (value) {
                if (empty(value))
                    return "0 元";
                // var price = parseFloat(value / 100).toFixed(2);
                return value + " 元";
            }
        },
        {
            field: 'notes',
            title: '订单说明',
            align: 'center',
            valign: 'middle',
            visible: false
        },
        {
            field: 'payorderno',
            title: '支付回调流水号（回调单号）',
            align: 'center',
            valign: 'middle',
            visible: false
        },
        {
            field: 'out_refund_no',
            title: '退款单号',
            align: 'center',
            valign: 'middle',
            // visible: false
        },
        {
            field: 'settlement_refund_fee',
            title: '应退款金额',
            align: 'center',
            valign: 'middle',
            // visible: false,
            formatter: function (value) {
                if (empty(value))
                    return "0 元";
                // var price = parseFloat(value / 100).toFixed(2);
                return value + " 元";
            }
        },
        {
            field: 'refund_fee',
            title: '微信实退款金额',
            align: 'center',
            valign: 'middle',
            // visible: false,
            formatter: function (value) {
                if (empty(value))
                    return "0 元";
                // var price = parseFloat(value / 100).toFixed(2);
                return value + " 元";
            }
        },
        {
            field: 'refund_desc',
            title: '退款原因',
            align: 'center',
            valign: 'middle',
            // visible: false
        },
        {
            field: 'refund_id',
            title: '微信退款单号',
            align: 'center',
            valign: 'middle',
            visible: false
        },
        {
            field: 'gmt_modified',
            title: '更新时间',
            align: 'center',
            // visible: false,
            formatter: function (value, row, index) {
                if (empty(value)) {
                    return "-";
                }
                return formatDateTime(value);
            }
        },
        {
            field: 'gmt_create',
            title: '创建时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                if (empty(value)) {
                    return "-";
                }
                return formatDateTime(value);
            }
        },
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNo: params.pageNumber,
            pageSize: params.pageSize,
            companyId: companyId,
            schoolId: schoolId,
            type: $("#type").val(),
            struts: $("#struts").val(),
            searchText: params.searchText,
            sortName: params.sortName,      //排序列名
            sortOrder: params.sortOrder //排位命令（desc，asc）
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.actArt_paper_list, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': 'paper:list'
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            console.log(res);
            if (!empty(res.strutsList)) {
                var map = res.strutsList;
                var strutsList = [];
                for(var key in map){
                    var obj = {};
                    obj.id = key;
                    obj.val = map[key];
                    if (!empty(map[key])&&'-'!=map[key]&&key<0) {
                        strutsList.push(obj);
                    }
                }
                app.initStrutsList(strutsList);
            }
            if (!empty(res.typeList)) {
                var map = res.typeList;
                var typeList = [];
                for(var key in map){
                    var obj = {};
                    obj.id = key;
                    obj.val = map[key];
                    if (!empty(map[key])&&'-'!=map[key]) {
                        typeList.push(obj);
                    }
                }
                app.initTypeList(typeList);
            }

            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入订单号/商品名称/会员名、电话搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        typeList: [],
        strutsList: [],
        permissions: ['paper:list', 'paper:infoList']
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        selectStatus: function () {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
        initStrutsList: function(list) {
            var that = this;
            that.strutsList = list;
        },
        initTypeList: function(list) {
            var that = this;
            that.typeList = list;
        },
        refund: function (index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            list = list[0];

            var orderId  =list.order_id;
            var loginType  =list.login_type;
            var appId  =list.app_id;

            var confirm = layer.confirm('确定退款？', {
                btn: ['确定', '拒绝'], //按钮
            }, function(){
                // 确定退款
                Get(that, _HOST + _API.wechat_pay_refund+"?orderId="+orderId+"&loginType="+loginType+"&appId="+appId, {}, true, "")
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response);
                        layer.close(confirm);
                        if (response) {
                            if (response.success) {
                                layer.msg(response.msg, {time: 1500, icon: 1});
                                setTimeout(function () {
                                    window.location.reload(); //刷新父页面
                                    layer.closeAll();
                                }, 1500)
                            } else {
                                layer.msg(response.msg, {time: 1500, icon: 5});
                            }
                        }
                    })
            }, function(){
                // 拒绝退款
                layer.close(confirm);
                layer.prompt({
                    formType: 2,
                    value: '',
                    title: '请输入拒绝原因',
                    area: ['500px', '250px'] //自定义文本域宽高
                }, function (value, index, elem) {
                    layer.close(index);
                    //得到value
                    Post(that, _HOST + _API.actArt_paper_refuseRefund+"?refuseDesc="+value+"&orderId="+orderId
                        ,{}, true, "refund:refund").then(function (response) {
                        // 响应成功回调
                        console.log(response);
                        if (response) {
                            if (response.success) {
                                layer.msg(response.msg, {time: 1500, icon: 1});
                                setTimeout(function () {
                                    window.location.reload(); //刷新父页面
                                    layer.closeAll();
                                }, 1500)
                            } else {
                                layer.msg(response.msg, {time: 1500, icon: 5});
                            }
                        }
                    });
                });
            });
        }
    },
});

$(function () {
    //初始化表格
    tableInit();
});

